package com.ohuang.hilt

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.ohuang.base.AuthInterceptorOkHttpClient
import com.ohuang.base.User
import com.ohuang.base.UserFactory

import com.ohuang.hilt.data.DataHelper
import com.ohuang.hilt.ui.theme.HiltTheme
import com.ohuang.login.LoginActivity
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.EntryPointAccessors
import okhttp3.OkHttpClient
import okhttp3.Request
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    @Inject
    lateinit var dataHelper: DataHelper

    @Inject
    @AuthInterceptorOkHttpClient
    lateinit var okHttpClient: OkHttpClient
    val TAG = "MainActivity"
    val mainViewModel: MainViewModel by viewModels()

    @Inject
    lateinit var user: User

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataHelper.put("ss", "ss")
//        DaggerHttpComponent.builder().context(this).build().inject(this)
        okHttpClient.newCall(request = Request.Builder().url("http://www.baidu.com").build())
        Log.d(TAG, "onCreate: " + okHttpClient)
        Log.d(TAG, "onCreate: "+user)
        mainViewModel.init()

        setContent {
            HiltTheme {

                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {

                    Greeting("Android$mainViewModel", Modifier.clickable {
                        startActivity(Intent(this@MainActivity, LoginActivity::class.java))
                    })
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    HiltTheme {
        Greeting("Android")
    }
}